package com.wgts.wgts.mvp.view.activity.store;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.control.recycleview.BaseLoadMoreAdapter;
import com.wgts.wgts.netmodle.resp.ShopKeeperListEntity;
import com.wgts.wgts.tools.CustomImageView;
import com.wgts.wgts.tools.MoHuGlide;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/*
 *
 *
 *   data:2018/10/1119:22
 *
 */public class StoreAdapter extends BaseLoadMoreAdapter<StoreAdapter.ViewHold> {
    private Context context;
    private List<ShopKeeperListEntity.Product> list;
    //判断是否是自己的店铺，从而是否展示下架按钮
    private String userType;

    private OnClickListener onClickListener;

    private cancelClickListen cancelClickListen;

    public StoreAdapter(Context context, List<ShopKeeperListEntity.Product> list, String userType) {
        super(context);
        this.context = context;
        this.list = list;
        this.userType = userType;
    }

    @Override
    public void onBindBodyViewHolder(@NonNull ViewHold viewHolder, final int i) {
        CustomImageView.getInstance().customView(context, viewHolder.shoppingCarItemImage, 350/(double)750);
        MoHuGlide.getInstance().glideImg(context, list.get(i).productSmallImage, viewHolder.shoppingCarItemImage);
        viewHolder.shoppingCarTitle.setText(list.get(i).productName);
        viewHolder.shoppingCarPrice.setText("￥" + list.get(i).retailPrice);
        viewHolder.marketPrice.setText(list.get(i).marketPrice);
        viewHolder.sallNumber.setText("已售" + list.get(i).sellCount + "件");
        if (userType != null && !userType.equals("")) {
            viewHolder.goBuyLy.setVisibility(View.GONE);
            viewHolder.shareLy.setVisibility(View.VISIBLE);
            viewHolder.cancelCollectLy.setVisibility(View.VISIBLE);
            viewHolder.cancelCollectLy.setClickable(true);
        } else {
            viewHolder.goBuyLy.setVisibility(View.VISIBLE);
            viewHolder.shareLy.setVisibility(View.GONE);
            viewHolder.cancelCollectLy.setVisibility(View.GONE);
            viewHolder.cancelCollectLy.setClickable(false);
        }

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onClickListener != null){
                    onClickListener.onClick(v, i);
                }
            }
        });

        viewHolder.cancelCollectLy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cancelClickListen != null){
                    cancelClickListen.cancelClick(v, i);
                }
            }
        });


    }

    @NonNull
    @Override
    public ViewHold onCreateBodyViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ViewHold(LayoutInflater.from(context).inflate(R.layout.store_recycle_item, viewGroup, false));
    }


    @Override
    public int getBodyItemCount() {
        return list.size();
    }

    @Override
    public int getBodyItemViewType(int position) {
        return 0;
    }

    public class ViewHold extends RecyclerView.ViewHolder {
        @BindView(R.id.shopping_car_item_image)
        ImageView shoppingCarItemImage;
        @BindView(R.id.shopping_car_title)
        TextView shoppingCarTitle;
        @BindView(R.id.sall_number)
        TextView sallNumber;
        @BindView(R.id.shopping_car_price)
        TextView shoppingCarPrice;
        @BindView(R.id.market_price)
        TextView marketPrice;
        @BindView(R.id.share_ly)
        RelativeLayout shareLy;
        @BindView(R.id.cancel_collect_ly)
        RelativeLayout cancelCollectLy;
        @BindView(R.id.go_buy_text)
        TextView goBuyText;
        @BindView(R.id.go_buy_ly)
        RelativeLayout goBuyLy;

        public View itemView;

        public ViewHold(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.itemView = itemView;
        }
    }

    public interface OnClickListener{
        void onClick(View view, int i);
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public interface cancelClickListen{
        void cancelClick(View view, int position);
    }

    public void setCancelClickListen(cancelClickListen cancelClickListen){
        this.cancelClickListen = cancelClickListen;
    }
}
